package cn.com.cym.gamezao.web.demo;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.BaseController;
import cn.com.cym.gamezao.base.BaseService;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.entity.demo.Child;
import cn.com.cym.gamezao.entity.demo.SingleChild;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.service.demo.SingleChildService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(tags = "x-oneByOne子类控制类")
@RestController
@RequestMapping(value = "single")
@AuthToken(require = false)
public class SingleChildController extends BaseController<SingleChild> {

    final SingleChildService thisService;

    public SingleChildController(SingleChildService singleChildService){
        this.thisService=singleChildService;
    }
    @Override
    public BaseService<SingleChild> getService() {
        return this.thisService;
    }

    @ApiImplicitParam(name = "child", value = "child", required = true,dataType = "SingleChild")
    @ApiOperation(value = "新建子类数据")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PostMapping()
    public ResponseEntity<?> add(@RequestBody SingleChild child){
        if(child.getFather()==null){
            throw new BaseException("不存在主表数据，无法新建",new ResultResponse("不存在主表数据，无法新建", HttpStatus.BAD_REQUEST.value()));
        }
        final SingleChild storeChild = this.thisService.save(child);
        return ResponseEntity.ok(storeChild);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id",paramType = "path", required = true,dataType = "Long"),
            @ApiImplicitParam(name = "singleChild", value = "singleChild",paramType = "", required = true,dataType = "SingleChild")})
    @ApiOperation(value = "修改子类数据")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PutMapping("/{id}")
    public ResponseEntity<?> update(@RequestBody SingleChild singleChild,@PathVariable Long id){
        if(singleChild.getFather()==null){
            throw new BaseException("不存在主表数据，无法更新",new ResultResponse("不存在主表数据，无法更新", HttpStatus.BAD_REQUEST.value()));
        }
        final SingleChild storeChild = this.thisService.update(singleChild,id);
        return ResponseEntity.ok(storeChild);
    }

}
